﻿using CommDll;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebServiceComm.Model;

namespace InstitutionAssitant
{
    class DataDeal
    {
        /// <summary>
        /// 格式化DataGridView特殊列
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="bIsFormatColumn">是否格式化列处理</param>
        public static void _formatDataGridViewColumn(DataGridView dgv, Boolean bIsFormatColumn = true)
        {
            //设置特定值
            string strTemp = "";
            foreach (DataGridViewRow dr in dgv.Rows)
            {
                if (bIsFormatColumn)
                {
                    foreach (DataGridViewCell cell in dr.Cells)
                    {
                        strTemp = cell.Value.ToString();
                        strTemp = (strTemp == null) ? "" : strTemp;
                        switch (dgv.Columns[cell.ColumnIndex].Name)
                        {
                            case "DownloadStatus":
                                cell.Value = strTemp.Equals("1") ? "已下载" : "未下载";
                                break;
                            case "UploadStatus":
                                cell.Value = strTemp.Equals("1") ? "已上传" : "未上传";
                                break;
                            case "DealStatus":
                                cell.Value = strTemp.Equals("1") ? "已处理" : "未处理";
                                break;
                            case "StockInStatus":
                                cell.Value = strTemp.Equals("1") ? "已入库" : "未入库";
                                break;
                            case "SendStatus":
                                cell.Value = strTemp.Equals("1") ? "已推送" : "未推送";
                                break;
                            case "ReceiveStatus":
                                switch (strTemp)
                                {
                                    case "0":
                                        cell.Value = "未收";
                                        break;
                                    case "1":
                                        cell.Value = "部收";
                                        break;
                                    case "2":
                                        cell.Value = "全收";
                                        break;
                                }
                                break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 改变DataGridView特殊列数据类型
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="bIsEachColumn">是否每一列处理</param>
        public static DataTable _changeDataTableColumnType(DataTable dt, Boolean bIsFormatColumn = false, string strColumnName = "")
        {
            DataTable dtResult = new DataTable();
            if (bIsFormatColumn)
            {
                //克隆表结构
                dtResult = dt.Clone();
                Boolean bTemp = false;
                strColumnName = "DownloadStatus,UploadStatus,DealStatus,StockInStatus,ReceiveStatus";
                foreach (DataColumn dc in dtResult.Columns)
                {
                    if (string.Format(",{0},", strColumnName).ToLower().Contains(string.Format(",{0},", dc.ColumnName).ToLower()) && (dc.DataType != typeof(String)))
                    {
                        //修改列类型
                        dc.DataType = typeof(String);
                        bTemp = true;
                        dc.ReadOnly = false;

                    }
                }
                // 有需改字段类型的数据
                if (bTemp)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        DataRow rowNew = dtResult.NewRow();
                        foreach (DataColumn dc in dtResult.Columns)
                        {
                            if (string.Format(",{0},", strColumnName).ToLower().Contains(string.Format(",{0},", dc.ColumnName).ToLower()) && (dc.DataType != typeof(String)))
                            {
                                rowNew[dc.ColumnName] = row[dc.ColumnName].ToString();
                            }
                            else
                            {
                                rowNew[dc.ColumnName] = row[dc.ColumnName];
                            }
                        }
                        dtResult.Rows.Add(rowNew);
                    }
                }
                else
                {
                    dtResult = dt;
                }
            }
            else
            {
                dtResult = dt;
            }
            return dtResult;
        }

        /// <summary>
        /// 取库房信息
        /// </summary>
        public static void _getWarehouse()
        {
            string strSql = "";
            if (AppInfo.m_UserInfo.m_strCode.Trim() == "")
            {
                strSql = "select Name +'|'+ Code as Name from T_Base_Warehouse";
            }
            else
            {
                strSql = "select storehouseName+'|'+storehouseCode as Name from His_User where code='" + AppInfo.m_UserInfo.m_strCode + "'";
            }
            AppInfo.m_dtWarehouse = DbHelper.DBAccess[0]._openSql_Dt(strSql);

        }
    }
}
